import scrapy
from yangguang.items import YangguangItem


class YgSpider(scrapy.Spider):
    name = 'yg'
    allowed_domains = ['sun0769.com']
    start_urls = ['http://wz.sun0769.com/political/index/supervise?page=1']

    def parse(self, response):
        #分组
        li_list = response.xpath("//ul[@class='title-state-ul']/li")
        for li in li_list:
            item = YangguangItem()
            item["title"] = li.xpath("./span[3]/a/text()").extract_first()
            item["herf"] = "http://wz.sun0769.com"+li.xpath("./span[3]/a/@href").extract_first()
            item["publish_date"] = li.xpath("./span[last()]/text()").extract_first()

            yield scrapy.Request(
                item["herf"],
                callback=self.parse_detail,
                meta = {"item":item}
            )

        #翻页
        next_url_xpath =  response.xpath("//div[@class='mr-three paging-box']/a[2]/@href")[0].extract()
        next_url = "http://wz.sun0769.com/"+next_url_xpath
        if next_url_xpath is not None:
            yield scrapy.Request(
                next_url,
                callback=self.parse
            )


    def parse_detail(self,response): #处理详情页
        item = response.meta["item"]
        item["content"] = response.xpath("//div[@class='details-box']/pre/text()").extract()
        item["content_img"] = response.xpath("//div[@class='clear details-img-list Picture-img']/img/@src").extract()
        # print (item)
        yield item





